package com.huonan.utils;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;

import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;

public class DateUtils {

    public static int getCurrentDayOfYear() {
        LocalDate now = LocalDate.now();
        return now.getDayOfYear();

    }

    public static int getCurrentMonth() {
        LocalDate now = LocalDate.now();
        return now.getMonthValue();
    }

    public static int getCurrentDayOfMonth() {
        LocalDate now = LocalDate.now();
        return now.getDayOfMonth();
    }

    public static String getCurrentDate() {
        LocalDate now = LocalDate.now();
        return now.toString();
    }

    public static String formatDate(String strDate){
        DateTime dateTime = DateUtil.parseDate(strDate);
        return DateUtil.format(dateTime, "yyyyMMdd");
    }

    public  static void parseDate(){
        String dateTimeString = "2023-03-17 15";
        String formatDate = formatDate(dateTimeString);
        System.out.println(formatDate);
//
//        String input = "2023-03-17T15:30:00";
//        DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE_TIME;
//        LocalDateTime dateTime = LocalDateTime.parse(input, formatter);
//        String formattedDateTime = dateTime.format(DateTimeFormatter.ofPattern("yyyy-MM"));
//        System.out.println("Formatted date: " + formattedDateTime);
    }
    public static String datePlus(String oldDate, int sub) {
        DateTime date = DateUtil.parse(oldDate);
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        // 把日期往后增加一天,整数  往后推,负数往前移动
        calendar.add(Calendar.DATE, sub);
        // 这个时间就是日期往后推一天的结果
        Date newDate = calendar.getTime();
        return DateUtil.format(newDate, "yyyyMMdd");
    }
    /**
     * 根据年份获取所有日期，输入yyyy,输出yyyyMMdd
     * @param year
     * @return
     */
    public static LinkedList<String> getDaysInYear(int year) {
        LinkedList<String> dateList = new LinkedList<>();
        for (int month = 1; month <= 12; month++) {
            for (int day = 1; day <= 31; day++) {
                try {
                    LocalDate date = LocalDate.of(year, month, day);
                    String format = DateUtil.format(date.atStartOfDay(), "yyyyMMdd");
                    dateList.add(format);
                } catch (Exception e) {
                    // e.printStackTrace();
                }
            }
        }
        return dateList;
    }

    private static List<String> getDaysInMonth(String monthOfYear, String FORMAT) {
        DateTime start = DateUtil.parse(monthOfYear, "yyyyMM");
        String formatStart = DateUtil.format(start, "yyyyMM");
        int lastDayOfMonth = DateUtil.getLastDayOfMonth(start);
        DateTime endDay = DateUtil.parse(formatStart + lastDayOfMonth);
        String yyyyMMdd = "yyyyMMdd";
        return DateUtil.rangeToList(start, endDay, DateField.DAY_OF_MONTH)
                .stream()
                .map(d -> DateUtil.format(d, yyyyMMdd)).
                filter(d -> {
                    DateTime now = DateTime.now();
                    String nowFormat = DateUtil.format(now, yyyyMMdd);
                    DateTime nowParse = DateUtil.parse(nowFormat, yyyyMMdd);
                    DateTime timeParse = DateUtil.parse(d, yyyyMMdd);
                    int compare = DateUtil.compare(timeParse, nowParse);
                    return compare <= 0;
                }).map(l -> String.format(FORMAT, l)).collect(Collectors.toList());
    }

    private static List<String> getMothsInYear(String year, String FORMAT) {
        DateTime start = DateUtil.parse(year, "yyyy");
        DateTime dateTime = DateUtil.offsetMonth(start, 11);
        return DateUtil.rangeToList(start, dateTime, DateField.MONTH).stream()
                .map(l -> DateUtil.format(l, "yyyyMM")).map(l -> String.format(FORMAT, l))
                .collect(Collectors.toList());
    }

    public static void main(String[] args) {
        parseDate();
    }

}